{% extends 'base.html' %}

{% block scripts %}
    <script src="{{ url_for('static', filename='js/custom.js') }}"></script>
    <script>
        // 将此 JavaScript 代码块移到 custom.js 文件中更佳
        function previewImage(input) {
            var preview = document.getElementById('imagePreview');
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                };

                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
{% endblock %}

{% block content %}
    <h2>{% block title %} 新建文章 {% endblock %}</h2>

    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="title">标题</label>
            <input type="text" name="title" placeholder="标题" class="form-control"
                   value="{{ request.form['title'] }}">
        </div>

        <div class="form-group">
            <label for="content">内容</label>
            <textarea name="content" placeholder="文章内容"
                class="form-control">{{ request.form['content'] }}</textarea>
        </div>

        <div class="form-group">
            <label for="imageUpload">选择要上传的图片：</label>
            <input type="file" id="imageUpload" name="image" accept="image/*" onchange="previewImage(this)">
        </div>

        <div class="form-group">
            <img id="imagePreview" src="#" style="display: none; max-width: 100%; height: auto;">
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-primary">提交</button>
        </div>
    </form>
{% endblock %}
